package html;

import java.io.IOException;
import java.sql.SQLException;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import db.DBConnection;
import config.HtmlUrls;

/**
 * @author mblank
 * @since  2012/1/18
 */
public class Artist {
	
	public Integer  id ;
	public String name;
	public int  following;
	public int  follower;
	public int  song_num;
	public int  album_num;
	
	private String artistsUrl;
	public Document doc;
	
	
	public Artist(int inid){
		id = inid;
		artistsUrl = getArtistsUrls();
		doc = getDoc();
	}
	
	/**
	 * get base url from xml config file (config/HtmlUrls.xml)
	 * @return 
	 */
	public String getArtistsUrls(){
		HtmlUrls hu = new HtmlUrls();
		String res = "";
		res = hu.getArtistsUrls();
		return res;
	}
	
	
	/**
	 * get the "doc"
	 * @return
	 */
	public Document getDoc(){
		Document localdoc = null;
		try {
			localdoc =  Jsoup.connect(artistsUrl+id.toString()).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return localdoc;
	}
	
	
	/**
	 * get the title of the HTML
	 * @return
	 */
	public String getTitle(){
		
		String title = "";
		if(doc!=null){
			title = doc.title();
		}
		return title; 
	}
	
	/**
	 * get string of the html
	 * @return
	 */
	public String getHtmlCode(){
		String htmlsource = "";
		if(doc!=null){
			htmlsource = doc.html();
		}		
		return htmlsource;
	}
	
	/**
	 * get the artist's name from the html
	 * @return
	 */
	public String getName(){
		
		Element name = doc.select("span[class]").get(4); 
		String res = "";
		res = name.text();	
		return res;	
	}
	
	/**
	 * get follower from the html
	 * @return
	 */
	public int getFollower(){
		
		String res = "";
		Elements followers = doc.getElementsByTag("a");		
		for(Element follower : followers){
			if(follower.attr("href").equals("/artist/"+id.toString()+"/follower")){
				Element temp  = follower.getElementsByTag("strong").first();
				res = temp.text();				
			}
		}
		return Integer.valueOf(res);		
	}
	
	/**
	 * get following from the html
	 * @return
	 */
	public int getFollowing(){
		
		String res = "";
		Elements followings = doc.getElementsByTag("a");		
		for(Element following : followings){
			if(following.attr("href").equals("/artist/"+id.toString()+"/following")){
				Element temp  = following.getElementsByTag("strong").first();
				res = temp.text();				
			}
		}
		return Integer.valueOf(res);
	}
	
	/**
	 * get the song_num from html
	 * @return
	 */
	public int getSongNum(){
		String res = "";
		Elements songs = doc.getElementsByTag("a");		
		for(Element song : songs){
			if(song.attr("href").equals("/artist/"+id.toString()+"/song")){
				Element temp  = song.getAllElements().first();
				
				if(temp==null){
					System.out.println("song_num null");
				}
				
				res = temp.text();				
			}
		}
		return Integer.valueOf(res);
	}
	
	/**
	 * get the album_num from html
	 * @return
	 */
	public int getAlbumNum(){
		String res = "";
		Elements albums = doc.getElementsByTag("a");		
		for(Element album : albums){
			if(album.attr("href").equals("/artist/"+id.toString()+"/album")){
				Element temp  = album.getAllElements().first();
				res = temp.text();				
			}
		}
		return Integer.valueOf(res);
	}
	
	/**
	 * find the error page from html
	 * @return
	 */
	public boolean checkError(){
		boolean check = false;
		Elements errors = doc.getElementsByTag("div");
		for(Element album : errors){
			if(album.attr("class").equals("error-text")){
				check = true;
			}
		}
		return check;
	}
	
	/**
	 * insert into the mysql(artist)
	 * @param id
	 * @param name
	 * @param follower
	 * @param following
	 * @param song_num
	 * @param album_num
	 * @return
	 */
	public int insert(int id,String name,int follower,int following,int song_num,int album_num){
		int state = 0;
		DBConnection dbc;
		try {
			dbc = new DBConnection();
			state = dbc.insertArtist(id, name, follower, following, song_num, album_num);
			dbc.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return state;
	}
	
	
	
	
	public void startRun(){
		if(doc!=null&&!(checkError())){
			name = getName();
			follower = getFollower();
			following = getFollowing();
			song_num = getSongNum();
			album_num = getAlbumNum();	
			insert(id,name,follower,following,song_num,album_num);
			System.out.println(id+"--"+name+"--"+follower+"--"+following+"--"+song_num+"--"+album_num);
		}else {
			System.out.println(id+"------none");
		}
		
	}
	
	

}
